﻿
    using Chain.BLL;
    using Chain.Model;
//    using ChainStock.Controls;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Wuqi.Webdiyer;

    public partial class GoodsList : PageBase
    {
        private int _curShopID;


        protected void btnGoodsListQuery_Click(object sender, EventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = 1;
            this.GetGoodsList(this.QueryCondition());
        }

        protected void btnOut_Click(object sender, EventArgs e)
        {
            Chain.BLL.Goods bllGoods = new Chain.BLL.Goods();
            int Counts = this.NetPagerParameter.RecordCount;
            string strSql = this.QueryCondition() + " and Goods.GoodsClassID = GoodsClass.ClassID and Goods.GoodsID = GoodsNumber.GoodsID";
            strSql = PubFunction.GetShopAuthority(base._UserShopID, "ShopID", strSql);
            DataTable dtGoods = bllGoods.GetListSP(0x186a0, 1, out Counts, new string[] { strSql }).Tables[0];
            DataExcelInfo.GoodsListExcel(dtGoods, base._UserName);
        }

        protected void drpPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = 1;
            this.NetPagerParameter.PageSize = int.Parse(this.drpPageSize.SelectedValue);
            this.GetGoodsList(this.QueryCondition());
        }

        private void GetGoodsList(string strSql)
        {
            Chain.BLL.Goods bllGoods = new Chain.BLL.Goods();
            int Counts = this.NetPagerParameter.RecordCount;
            strSql = strSql + " and Goods.GoodsClassID = GoodsClass.ClassID and Goods.GoodsID = GoodsNumber.GoodsID and GoodsNumber.ShopID=" + this.sltShop.Value;
            DataTable dtGoods = bllGoods.GetListSP(this.NetPagerParameter.PageSize, this.NetPagerParameter.CurrentPageIndex, out Counts, new string[] { strSql }).Tables[0];
            this.NetPagerParameter.RecordCount = Counts;
            this.NetPagerParameter.CustomInfoHTML = string.Format("<div class=\"results\"><span>当前第{0}/{1}页 共{2}条记录 每页{3}条</span></div>", new object[] { this.NetPagerParameter.CurrentPageIndex, this.NetPagerParameter.PageCount, this.NetPagerParameter.RecordCount, this.NetPagerParameter.PageSize });
            this.gvGoodsList.DataSource = dtGoods;
            this.gvGoodsList.DataBind();
            PageBase.BindSerialRepeater(this.gvGoodsList, this.NetPagerParameter.PageSize * (this.NetPagerParameter.CurrentPageIndex - 1));
        }

        protected void NetPagerParameter_PageChanging(object src, PageChangingEventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = e.NewPageIndex;
            this.GetGoodsList(this.QueryCondition());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                
                PubFunction.BindShopSelect(base._UserShopID, this.sltShop, base._UserShopID, base._UserShopID != 1);
                this.GetGoodsList(this.QueryCondition());
            }
        }

        protected string QueryCondition()
        {
            string strQueryGoods = this.txtQueryGoods.Value;
            string strGoodsClass = (base.Request["sltGoodsClass"] == null) ? "" : base.Request["sltGoodsClass"].Trim();
            string strShopId = this.sltShop.Value;
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" Goods.GoodsID >0 ");
            if (strQueryGoods != "")
            {
                strSql.AppendFormat("and (Name like '%{0}%' or NameCode like '%{0}%' or GoodsCode like '%{0}%')", strQueryGoods);
            }
            
            if (strGoodsClass != "")
            {
                if (new Chain.BLL.GoodsClass().GetModel(int.Parse(strGoodsClass)).ParentID == 0)
                {
                    string strClass = int.Parse(strGoodsClass) + ",";
                    DataTable dt = new Chain.BLL.GoodsClass().GetList("ParentID=" + int.Parse(strGoodsClass)).Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        strClass = strClass + dt.Rows[i]["ClassID"] + ",";
                    }
                    int length = strClass.Length;
                    strClass = strClass.Substring(0, length - 1);
                    strSql.AppendFormat(" and GoodsClassID in ({0})", strClass);
                }
                else
                {
                    strSql.AppendFormat(" and GoodsClassID={0}", int.Parse(strGoodsClass));
                }
            }
            if (strShopId != "")
            {
                strSql.AppendFormat(" and GoodsNumber.ShopID={0}", int.Parse(strShopId));
            }
            return strSql.ToString();
        }

        public int CurShopID
        {
            get
            {
                if (this._curShopID == 0)
                {
                    this._curShopID = base._UserShopID;
                    if (!string.IsNullOrEmpty(this.sltShop.Value))
                    {
                        this._curShopID = int.Parse(this.sltShop.Value);
                    }
                }
                return this._curShopID;
            }
        }
    }


